-- This file is hereby placed in the Public Domain
local addonName, ns = ...
local addon = ns[addonName]
local oUF = ns.oUF
local utils = addon.utils
local tags = addon.tags
local cfg = addon.cfg

local localized, class = UnitClass('player')
local colors = addon.colors

---------------------Druid Power------------------------------
local function updateDruidPower(self, event, unit)
   if(unit and unit ~= self.unit) then return end
   local bar = self.DruidPower
   
   local mana = UnitPowerType('player') == 0
   local min, max = UnitPower('player', mana and 3 or 0), UnitPowerMax('player', mana and 3 or 0)

   bar:SetStatusBarColor(unpack(colors.power[mana and 'ENERGY' or 'MANA']))
   bar:SetMinMaxValues(0, max)
   bar:SetValue(min)
   bar:SetAlpha(min ~= max and 1 or 0)
end

---------------------Monk Power------------------------------
--[[local UpdateChi = function(self, event, unit, powerType)
	if self.unit ~= unit or (powerType and powerType ~= "LIGHT_FORCE") then return end
	local num = UnitPower("player", SPELL_POWER_LIGHT_FORCE)
	local numMax = UnitPowerMax("player", SPELL_POWER_LIGHT_FORCE)
	for i = 1, numMax do
		if i <= num then
			self.Harmony[i]:SetAlpha(1)
		else
			self.Harmony[i]:SetAlpha(0)
		end
	end
     end
]]
local function targetChangedSound(self, unit)
    self:RegisterEvent('PLAYER_TARGET_CHANGED', function(self)
        if UnitExists('target') then
            if( UnitIsEnemy('target', 'player') ) then
                PlaySound('igCreatureAggroSelect')
            elseif( UnitIsFriend('player', 'target') ) then
                PlaySound('igCharacterNPCSelect')
            else
                PlaySound('igCreatureNeutralSelect')
            end
        else
            PlaySound('INTERFACESOUND_LOSTTARGETUNIT')
        end
    end)
end

addon:addLayoutElement('player', targetChangedSound)

local textures = {
    [1] = utils.mediaPath .. 'PlayerFrame',
    [2] = utils.mediaPath .. 'PlayerLowThreat',
    [3] = utils.mediaPath .. 'PlayerHighThreat',
}
local function onevent(self, event, unit)
    if(self.unit ~= unit) then return end
    local status = UnitThreatSituation(unit) or 0
    local texture = textures[status] or textures[1]
    self.texture:SetTexture(texture)
end
local function threatUpdate(self, unit)
    self:RegisterEvent('UNIT_THREAT_SITUATION_UPDATE', onevent)
    table.insert(self.__elements, onevent)
end

addon:addLayoutElement('player', threatUpdate)

--local PlayerStyle = function(self, unit)
local function PlayerStyle(self, unit)
   local hp = self:CreateFontString(nil, 'OVERLAY')
   hp:SetFont(STANDARD_TEXT_FONT, 16, 'OUTLINE')
   hp:SetPoint('RIGHT', self.Health, -5, 0)
   hp:SetJustifyH('RIGHT')
   self:Tag(hp, tags.smartHP)

   local mp = self:CreateFontString(nil, 'OVERLAY')
   mp:SetFont(STANDARD_TEXT_FONT, 16, 'OUTLINE')
   mp:SetPoint('LEFT', self.Health, 5, 0)
   mp:SetJustifyH('LEFT')
   self:Tag(mp, tags.smartMP)

   local lvl = self:CreateFontString(nil, 'OVERLAY')
   lvl:SetFont(STANDARD_TEXT_FONT, 14, 'OUTLINE')
   lvl:SetPoint('LEFT', 12, 25)
   lvl:SetJustifyH('CENTER')
   self:Tag(lvl, "[diffcolor][level][shortclassification]")

   local name = self:CreateFontString(nil, 'OVERLAY')
   name:SetFont(STANDARD_TEXT_FONT, 16, 'OUTLINE')
   name:SetPoint('LEFT', self.Health, 2, 35)
   self:Tag(name, '[smartINFO][shortlarge]')-- tags.majorname)

-- oUF Swing
   if cfg.show.swing then
      self.Swing = CreateFrame('StatusBar', nil, self)
      self.Swing:SetPoint('CENTER', UIParent, 0, -320)
      self.Swing:SetHeight(6)
      self.Swing:SetWidth(110)
      self.Swing:SetStatusBarTexture(utils.mediaPath.."nixieBar2" )
      local r, g, b
      local class = select(2, UnitClass('player'))
      if class then
	 r = RAID_CLASS_COLORS[class].r
	 g = RAID_CLASS_COLORS[class].g
	 b = RAID_CLASS_COLORS[class].b
      end
      self.Swing:SetStatusBarColor( r, g, b ,0.85 )
      self.Swing:SetBackdrop(Backdrop)
      self.Swing:SetBackdropColor(.15,.15,.15)
      self.Swing.disableMelee = true
	      
      self.Swing.Text = self:CreateFontString(nil, 'OVERLAY')
      self.Swing.Text:SetFont(STANDARD_TEXT_FONT, 16, 'OUTLINE')
      self.Swing.Text:SetPoint('CENTER', UIParent, 0, -300)--'CENTER', 0, 1)
      self.Swing.Text:SetTextColor(201/255,153/255,168/255)
	      
      self.Swing.bg = self.Swing:CreateTexture(nil, 'BORDER')
      self.Swing.bg:SetAllPoints(self.Swing)
      self.Swing.bg:SetTexture(utils.mediaPath.."rothTex" )
      self.Swing.bg:SetVertexColor(.15,.15,.15,0.15)
   end 

-- MONK bar
--[[   if class == 'MONK' then
      self.Harmony = {}
      self.Harmony = CreateFrame("Frame", nil, self.Power)
--      for i = 1, UnitPower("player", SPELL_POWER_LIGHT_FORCE) do
      for i = 1, UnitPowerMax("player", SPELL_POWER_LIGHT_FORCE) do
	 self.Harmony[i] = self.Power:CreateTexture(nil, 'OVERLAY')--"ARTWORK")
	 self.Harmony[i]:SetSize(20, 20)
	 self.Harmony[i]:SetTexture(utils.mediaPath.."bubbletex")
	 
	 if i == 1 then
	    self.Harmony[i]:SetPoint("LEFT", self, 110, 25)
	 else
	    self.Harmony[i]:SetPoint("LEFT", self.Harmony[i-1], "RIGHT", 10, 0)
	 end

	 self.Harmony[i]:SetVertexColor(1,1,.66)--0.71, 0.43, 0.27)
      end

      self.Harmony.Override = UpdateChi
   end
]]
end

addon:addLayoutElement('player', PlayerStyle)

--[[local function CreateMonkBar(self, unit)
   self.Harmony = {}
   self.Harmony = CreateFrame("Frame", nil, self.Power)
   for i = 1, UnitPowerMax("player", SPELL_POWER_LIGHT_FORCE) do
      self.Harmony[i] = self.Power:CreateTexture(nil, 'OVERLAY')--"ARTWORK")
      self.Harmony[i]:SetSize(20, 20)
      self.Harmony[i]:SetTexture(utils.mediaPath.."bubbletex")
	 
      if i == 1 then
	 self.Harmony[i]:SetPoint("LEFT", self, 110, 25)
      else
	 self.Harmony[i]:SetPoint("LEFT", self.Harmony[i-1], "RIGHT", 10, 0)
      end

      self.Harmony[i]:SetVertexColor(1,1,.66)--0.71, 0.43, 0.27)
   end
   
   self.Harmony.powerType = SPELL_POWER_LIGHT_FORCE
   self.Harmony.Override = UpdateChi
end

addon:addLayoutElement('player', class == 'MONK' and CreateMonkBar)]]

addon:addLayoutElement('player', function(self, unit)
    local combat = self:CreateTexture(nil, 'OVERLAY')
    self.Combat = combat
    utils.setSize(combat, 24)
    combat:SetTexture(utils.mediaPath .. 'sword')
    combat:SetPoint('LEFT', self, 'RIGHT', 3, 0)


    local looter = self:CreateTexture(nil, 'OVERLAY')
    self.MasterLooter = looter
    utils.setSize(looter, 24)
    --looter:SetTexture(utils.mediaPath .. '')
    looter:SetTexture([[Interface\GroupFrame\UI-Group-MasterLooter]])
    looter:SetPoint('CENTER', self, 'TOPLEFT', 10, 0)

    local leader = self:CreateTexture(nil, 'OVERLAY')
    self.Leader = leader
    utils.setSize(leader , 30)
    leader:SetTexture(utils.mediaPath .. 'Leader')
    leader:SetPoint('TOPRIGHT', self, -20, 22)

    local ass = self:CreateTexture(nil, 'OVERLAY')
    self.Assistant = ass
    utils.setSize(ass, 30)
    ass:SetTexture(utils.mediaPath .. 'assistant')
    ass:SetPoint('CENTER', leader)

end)

local druidPower = function(self, unit)
--[[    self.DruidPower = CreateFrame("StatusBar", nil, self)
    self.DruidPower:SetStatusBarTexture(utils.mediaPath.."nixieBar2" )
    self.DruidPower:SetHeight(2)
    self.DruidPower:SetPoint('TOPLEFT', self.Health, 'BOTTOMLEFT',3, 2)
    self.DruidPower:SetPoint('BOTTOMRIGHT', self.Health, 'BOTTOMRIGHT', -3, 0)
    self.DruidPower:SetAlpha(0)

    local value = self.DruidPower:CreateFontString(nil, 'OVERLAY')
    value:SetFont(STANDARD_TEXT_FONT, 16, 'OUTLINE')
    value:SetPoint('LEFT', self.DruidPower,'RIGHT',10,-5)
    self:Tag(value, '[druidpower]')

    table.insert(self.__elements, updateDruidPower)
    self:RegisterEvent('UNIT_MAXPOWER', updateDruidPower)
    self:RegisterEvent('UNIT_POWER', updateDruidPower)
    self:RegisterEvent('UPDATE_SHAPESHIFT_FORM', updateDruidPower)
    --]]
   local mana = CreateFrame('StatusBar', nil, self.Power)

    mana:SetPoint('TOPLEFT', self.Power)
    mana:SetPoint('TOPRIGHT', self.Power)
    mana:SetStatusBarTexture(utils.mediaPath.."nixieBar2")--media.roth)
    mana:SetHeight(6)

    mana.bg = mana:CreateTexture(nil, 'BORDER')
    mana.bg:SetAllPoints(mana)
    mana.bg:SetTexture(utils.mediaPath.."nixieBar2")--media.roth)
    mana.bg.multiplier = .3

    mana.frequentUpdates = true
    mana.colorPower = true

    self.DruidMana = mana
 end

addon:addLayoutElement('player', class == 'DRUID' and druidPower)

local MonkBar = function(self, unit)
local cicons = {}
    for i = 1, 5 do
        local f = self.Health:CreateTexture(nil, 'OVERLAY')
        cicons[i] = f
        f:SetSize(10, 10)
        f:SetTexture(utils.mediaPath.."bubbletex")
        if(i == 1) then
            f:SetPoint('TOPRIGHT', self, -40, 3)
        else
            f:SetPoint('LEFT', cicons[i-1], 'RIGHT', 1)
        end
        f:SetVertexColor(.33, .59, .33)
    end

    self.ClassIcons = cicons
 end

addon:addLayoutElement('player', class == 'MONK' and MonkBar)

addon:addLayoutElement('player', class == 'DEATHKNIGHT' and function(self, unit)
    --[[local runes = CreateFrame('Frame', nil, self.Power)
    self.Runes = runes

    --utils.testBackdrop(runes)

    local w,h, spacing = 180, 7, 1
    utils.setSize(runes, w, 7)
    runes:SetPoint('TOP', self, 37, -30)

    for i = 1, 6 do
        local bar = CreateFrame('StatusBar', nil, runes)
        runes[i] = bar

        bar:SetStatusBarTexture(utils.mediaPath .. 'StatusBar1')
        utils.setSize(bar, (w-5*spacing)/6, h)

        if(i==1) then
            bar:SetPoint('TOPLEFT', runes)
        else
            bar:SetPoint('TOPLEFT', runes[i-1], 'TOPRIGHT', spacing, 0)
        end

        bar.bg = bar:CreateTexture(nil, 'BORDER')
        bar.bg:SetAllPoints(bar)
        bar.bg:SetTexture(utils.mediaPath .. 'StatusBar1')
        bar.bg.multiplier = .5
     end]]

    local BAR_HEIGHT = 6

    local runes = CreateFrame('Frame', nil, self.Power)
    runes:SetPoint('TOPLEFT', self.Power)
    runes:SetPoint('TOPRIGHT', self.Power)
    runes:SetHeight(BAR_HEIGHT)

    local MAX_RUNES = 6
    local GAP = 1
    local bar_width = (self.Power:GetWidth() - GAP*(MAX_RUNES-1)) / MAX_RUNES

    for i = 1, MAX_RUNES do
        local bar = CreateFrame('StatusBar', nil, runes)
        runes[i] = bar
        bar:SetStatusBarTexture(utils.mediaPath .. 'StatusBar1')--media.roth)

        bar.bg = bar:CreateTexture(nil, 'BORDER')
        bar.bg:SetAllPoints(bar)
        bar.bg:SetTexture(utils.mediaPath .. 'StatusBar1')--media.roth)
        bar.bg.multiplier = .3

        bar:SetWidth(bar_width)
        bar:SetHeight(BAR_HEIGHT)
        if(i == 1) then
            bar:SetPoint('TOPLEFT', runes, 'TOPLEFT', 0, 0)
        else
            bar:SetPoint('TOPLEFT', runes[i-1], 'TOPRIGHT', GAP, 0)
        end
     end

     self.Runes = runes
end)




